package com.thzy.bbs.common;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.androidadvance.topsnackbar.TSnackbar;
import com.thzy.bbs.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 公用类
 *
 * @author
 * @date 2017/1/3 16:31
 */

public class CommonUtils {
    public static WebView webView;
    public static String session;

    private CommonUtils() {
    }

    /**
     * 获取sd卡路径，没有则获取内置内存路径
     *
     * @return
     */
    public static String getSDPath() {
        String path;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);// 判断sd卡是否存在
        if (sdCardExist) {
            File sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
            path = sdDir.toString();
        } else {
            path = Environment.getExternalStorageDirectory().getPath();
        }
        return path;
    }

    /**
     * 获取程序目录
     *
     * @param context
     * @return
     */
    public static String getApplicationPath(Context context) {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }

    /**
     * 判断网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
        } else {
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    /**
     * 调用前端方法地址拼接
     *
     * @return
     */
    public static String functionJoin(String functionName, String... params) {
        StringBuilder result = new StringBuilder();
        result.append("javascript:").append(functionName).append("(");
        for (int i = 0; i < params.length; i++) {
            if (i == params.length - 1) {
                result.append("'").append(params[i]).append("'");
            } else {
                result.append("'").append(params[i]).append("'").append(",");
            }
        }
        result.append(")");
        return result.toString();
    }

    /**
     * 消息弹框
     *
     * @param messages  消息内容
     * @param view      父级视图
     * @param times     时间(默认两秒)
     * @param bgColor   背景颜色
     * @param textColor 字体颜色
     */
    public static void showMessage(List<String> messages, View view, int times, Integer bgColor, Integer textColor) {
        if (messages.size() <= 0) {
            return;
        }
        if (times == 0) {
            times = 2000;
        }
        String msg = messages.get(0);
        messages.remove(0);
        showSnackbar(msg, view, times, messages, bgColor, textColor);
    }

    /**
     * 消息弹框（单条）
     *
     * @param messages  消息
     * @param view      视图
     * @param times     时间（默认两秒）
     * @param bgColor   背景颜色
     * @param textColor 文字颜色
     */
    public static void showMessage(String messages, View view, int times, Integer bgColor, Integer textColor) {
        List<String> msg = new ArrayList<>();
        msg.add(messages);
        showMessage(msg, view, times, bgColor, textColor);
    }

    /**
     * 显示消息
     *
     * @param messages  消息内容
     * @param view      父级视图
     * @param times     时间
     * @param bgColor   背景颜色
     * @param textColor 字体颜色
     */
    private static void showSnackbar(String message, final View view, final int times, final List messages, final Integer bgColor, final Integer textColor) {
        TSnackbar snackbar = TSnackbar.make(view, message, times);
        final View snackbarView = snackbar.getView();//获取Snackbar的view
        if (null != bgColor && null != textColor) {
            snackbarView.setBackgroundColor(bgColor);//修改view的背景色
            ((TextView) snackbarView.findViewById(R.id.snackbar_text)).setTextColor(textColor);//获取Snackbar的message控件，修改字体颜色
        }
        snackbar.setCallback(new TSnackbar.Callback() {
            @Override
            public void onDismissed(TSnackbar TSnackbar, int event) {
                super.onDismissed(TSnackbar, event);
                showMessage(messages, view, times, bgColor, textColor);
            }
        });
        snackbar.show();
    }

    /**
     * 获取软件版本号
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        int versionCode;
        try {
            // 获取软件版本号，
            versionCode = context.getPackageManager().getPackageInfo("com.thzy.bbs", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            versionCode = 0;
        }
        return versionCode;
    }
}
